package pers.apricot

import com.google.inject.Guice
import com.google.inject.Injector
import io.ebean.EbeanServer
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.RowMapper
import pers.apricot.base.MineModule
import pers.apricot.domain.CustomerDomain
import pers.apricot.domain.query.QCustomerDomain

import java.sql.ResultSet
import java.sql.SQLException

class Portal {
	static final Injector INJECTOR = Guice.createInjector(new MineModule())

	static void main(String[] args) {
		final int count = 1000
//		CustomerService customerService = INJECTOR.getInstance(CustomerService)
//		List<CustomerDomain> list = customerService.fuzzyFind(new CustomerDomain(name: 'abc'))
		for (int i = 0; i < count; i++) {
			QCustomerDomain qCustomerDomain = new QCustomerDomain()
			List<CustomerDomain> list = qCustomerDomain.findList()
		}
		long time1 = System.nanoTime()
		for (int i = 0; i < count; i++) {
			QCustomerDomain qCustomerDomain = new QCustomerDomain()
			List<CustomerDomain> list = qCustomerDomain.findList()
		}
		long time2 = System.nanoTime()
		JdbcTemplate jdbcTemplate = INJECTOR.getInstance(JdbcTemplate)
		RowMapper<CustomerDomain> rowMapper = new RowMapper<CustomerDomain>() {
			@Override
			CustomerDomain mapRow(ResultSet rs, int rowNum) throws SQLException {
				[id          : rs.getInt(1), version: rs.getLong(2), whenCreated: rs.getTimestamp(3).toInstant(),
				 whenModified: rs.getTimestamp(4).toInstant(), name: rs.getString(5)] as CustomerDomain
			}
		}
		for (int i = 0; i < count; i++) {
			List<CustomerDomain> list = jdbcTemplate.query(/select * from customer/, rowMapper)
		}
		long time3 = System.nanoTime()
		for (int i = 0; i < count; i++) {
			List<CustomerDomain> list = jdbcTemplate.query(/select * from customer/, rowMapper)
		}
		long time4 = System.nanoTime()
		println time2 - time1
		println time4 - time3
		EbeanServer ebeanServer = INJECTOR.getInstance(EbeanServer)
		for (int i = 0; i < count; i++) {
			CustomerDomain customerDomain = [name: 'test'] as CustomerDomain
			customerDomain.save()
//			SqlUpdate sqlUpdate = ebeanServer.createSqlUpdate(/insert into customer (name) values (?)/)
//			sqlUpdate.setParameter(1, 'test')
//			ebeanServer.execute(sqlUpdate)
		}
		long time5 = System.nanoTime()
		for (int i = 0; i < count; i++) {
//			SqlUpdate sqlUpdate = ebeanServer.createSqlUpdate(/insert into customer (name) values (?)/)
//			sqlUpdate.setParameter(1, 'test')
//			ebeanServer.execute(sqlUpdate)
			CustomerDomain customerDomain = [name: 'test'] as CustomerDomain
			customerDomain.save()
		}
		long time6 = System.nanoTime()
		for (int i = 0; i < count; i++) {
			jdbcTemplate.batchUpdate(/BEGIN /, /insert into customer (name) values ('test')/, /COMMIT /)
//			jdbcTemplate.update(/insert into customer (name) values (?)/, 'test')
		}
		long time7 = System.nanoTime()
		for (int i = 0; i < count; i++) {
			jdbcTemplate.batchUpdate(/BEGIN /, /insert into customer (name) values ('test')/, /COMMIT /)
//			jdbcTemplate.update(/insert into customer (name) values (?)/, 'test')
		}
		long time8 = System.nanoTime()
		println time6 - time5
		println time8 - time7
	}


}
